<template>
    <el-row class="edit">
        <el-container style="height: 100%;">
            <el-header height="60px">
                新增凭证
            </el-header>
            <el-main>
                <el-form ref="vouerData" :rules="rules" :model="vouerData" label-width="115px"
                         :label-position="formAlignLeft" size="mini">
                    <el-form-item label="凭证类型 :" prop="vou">
                        <el-input v-model="vouerData.vou" placeholder="请输入凭证类型"></el-input>
                    </el-form-item>
                    <el-form-item label="凭证代码 :" prop="code">
                        <el-input v-model="vouerData.code" placeholder="请输入凭证代码"></el-input>
                    </el-form-item>
                </el-form>
            </el-main>
            <el-footer height="80px">
                <el-row style="height: 20px;margin-bottom: 5px;">
                    <transition name="el-zoom-in-bottom">
                        <el-row v-show="infoShow" style="display: flex;justify-content: center;align-items: center;">
                            {{ (info !== '') ? "提示信息："+info : ''}}
                        </el-row>
                    </transition>
                </el-row>

                <el-row style="display: flex;justify-content: center;align-items: center;">
                    <el-button @click="submit" type="primary">
                        保存
                    </el-button>
                    <el-button @click="editOut">
                        取消
                    </el-button>
                </el-row>
            </el-footer>
        </el-container>
    </el-row>
</template>

<script>
    import {getDataForSP, writePageInfo, replaceSpacesObj} from '../../../utils/webUtils.js';

    export default {
        name: "addVouer",
        data() {
            return {
                formAlignLeft: 'left',
                vouerData: {
                    userUuid: this.user.uuid,
                    userName: this.user.name,
                    vou: "",
                    code: ""
                },
                insts: [],
                instObj: {},
                instPage: {
                    pageSize: 9999,//每页显示条目个数
                    currentPage: 1,//当前页数
                    totalElements: 0,
                    searchData: {}
                },
                rules: {
                    vou: [
                        {required: true, message: '凭证类型不得为空', trigger: 'blur'},
                    ],
                    code: [
                        {required: true, message: '凭证代码不得为空', trigger: 'blur'},
                    ]
                },
                info: '',
                infoShow: false
            };
        },
        props: {
            user: {
                type: Object
            }
        },
        watch: {
            user(val) {
                this.vouerData.userUuid = val.uuid;
                this.vouerData.userName = val.name;
            }
        },
        mounted() {
            console.log(this.user)
            this.queryInsts();
        },
        methods: {
            // instChg() {
            //     this.secgData.instName = this.instObj[this.secgData.instUuid];
            // },
            queryInsts: function () {
                this.axios.request({
                    method: "get",
                    url: "./kwp/kdi/inst/search/findAll",
                    params: getDataForSP(this.instPage)
                }).then(response => {
                    this.insts = response.data.content;
                    writePageInfo(this.instPage, response.data);
                    for (let i in this.insts) {
                        this.instObj[this.insts[i].uuid] = this.insts[i].name;
                    }
                }).catch(error => {
                    console.log(error);
                });
            },
            submit() {
                this.$refs['vouerData'].validate((valid) => {
                    if (!valid) {
                        return;
                    }
                    this.axios.request({
                        method: "post",
                        url: "./kwp/kdi/vouer",
                        headers: {
                            "Content-Type": "application/json;charset=UTF-8"
                        },
                        data: replaceSpacesObj(this.vouerData)
                    }).then(() => {
                        this.info = '新增成功,两秒后关闭';
                        this.infoShow = true;
                        setTimeout(() => {
                            this.infoShow = false;
                            this.$emit("editState", false);
                            this.cleanVouer();
                            this.info = '';
                        }, 2000);
                    }).catch(error => {
                        console.log(error);
                        this.info = "新增失败";
                        this.infoShow = true;
                    });
                });
            },
            cleanVouer() {
                this.vouerData = {
                    vou: "",
                    name: ""
                }
            },
            editOut() {
                this.infoShow = false;
                this.cleanVouer();
                this.info = '';
                this.$emit("editState", false);
            }
        }
    }
</script>

<style scoped>
    .edit {
        width: 360px;
        height: 100%;
        z-index: 100;
    }


</style>